unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Timer1: TTimer;
    Timer2: TTimer;
    Button1: TButton;
    ScrollBar1: TScrollBar;
    ScrollBar2: TScrollBar;
    Edit1: TEdit;
    Button2: TButton;
    Label1: TLabel;
    Label2: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure ScrollBar2Change(Sender: TObject);
    procedure ScrollBar1Change(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Timer2Timer(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    procedure Start;
  end;

Const
  W=30;
  H=30;
  D=10;
  K=10;
  E=5;

var
  Form1: TForm1;
  N, s, step, run, a, x1, x2, m1, m2 : integer;
  mass : array of integer;
  arr : array of TPanel;
  flag:boolean;
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var i : integer;
begin
  N := StrToInt (Edit1.Text);
  for i := 0 to N-1 do
  begin
    mass[i] := Random (N+10);
    arr[i].Caption :=inttostr(mass[i]);
    arr[i].color := clWhite;
  end;
  Timer1.Enabled := True;
  run := 0;
  step := 0;
  a:=0;


end;

procedure TForm1.Start;
var e : integer;
begin
  Randomize;
  for e:=0 to N-1 do
  begin
    arr[e].Color:=clWhite;
    arr[step].Color:=clRed;
    arr[step+1].Color:=clRed;
    if (run=N-1) then
      arr[e].Color:=clGreen;
  end;

  for e:=0 to N-1 do
  begin
    arr[e].Caption:=IntToStr(mass[e]);

    if (e >  N-run-1) then
    begin
      arr[e].color := clGreen;
    end;

   end;

end;

procedure TForm1.ScrollBar2Change(Sender: TObject);
begin
  Timer2.Interval := ScrollBar2.Position*10;
end;

procedure TForm1.ScrollBar1Change(Sender: TObject);
begin
  Timer1.Interval := ScrollBar1.Position*10;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin

  x1 := arr[step].Left;
  x2 := arr[step+1].Left;

  s:=0;
  if (mass[step] > mass[step+1]) or (flag=false) then
  begin
    if (s=0) then
    begin
      Timer1.Enabled := False;
      Timer2.Enabled := True;
      Exit;
    end;

  end;

  Start;
  step:=step+1;
  flag:=true;

  if (step = N-run-1) then
  begin
    step := 0;
    run:=run+1;
    if (run=N-1) then
    begin
      Timer1.Enabled := false;
      Start;
    end;
  end;


end;

procedure TForm1.Timer2Timer(Sender: TObject);
var j : integer;
begin
  Start;

  if ((arr[step+1].Left - arr[step].Left)<=(W+D)) and ((arr[step+1].Left - arr[step].Left)>=-5) and (flag) then
  begin
    arr[step].left := arr[step].left + E;
    arr[step+1].left := arr[step+1].left - E;
  end;

  if (arr[step].Left = arr[step+1].Left ) and (flag) then
  begin
                              {
    arr[step].Left := x1;
    arr[step+1].Left := x2;

    Start;                   }

    mass[step] := mass[step]+mass[step+1];
    mass[step+1] := mass[step]-mass[step+1];
    mass [step] := mass[step]-mass[step+1];

    arr[step].Caption := IntToStr(mass[step]);
    arr[step+1].Caption := IntToStr(mass[step+1]);
    flag:=false;

  end;

  if (flag=false) and ((arr[step+1].Left - arr[step].Left)<(W+D)) then
  begin
    arr[step].left := arr[step].left - E;
    arr[step+1].left := arr[step+1].left + E;
  end;

  if ((arr[step+1].Left - arr[step].Left)=(W+D)) and (flag=false) then
  begin

    Start;
    flag:=true;
  end;
  Timer2.Enabled := false;
  Timer1.Enabled := True;
  s:=1;

end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Timer1.Enabled:=False;
  Timer2.Enabled:=False;

end;

procedure TForm1.Button2Click(Sender: TObject);
var i : integer;
begin
  N := StrToInt (Edit1.Text);
  setlength (mass, N);
  setlength (arr, N);
  for i:=0 to N-1 do
  begin
    arr[i]:=TPanel.Create(Form1);
    arr[i].Width := W;
    arr[i].Height := H;
    arr[i].Parent := Form1;
    arr[i].Left := (W+D)*i+K;
    arr[i].Top := K;
  end;

  flag:=true;
end;

end.
